package gw.com.android.ui.positions;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.model.DataManager;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.StringFormatter;

/**
 * Created by reeta.zou on 2016/7/7.
 * 当日成交界面适配器
 */
public class DealAdapter extends RecylerListAdapter {

    private DataItemResult mList;
    private DataItemDetail mItem;

    /**
     * 初始化界面数据
     *
     * @param context
     */
    public DealAdapter(Context context, RecyclerView mView) {
        super(context, mView);
        mList = new DataItemResult();
        mList.appendItems(DataManager.instance().getSortOrderList(DataManager.instance().mDealInfoList));
        notifyDataChanged();
    }

    @Override
    public void refreshData() {
        mList.clear();
        mList.appendItems(DataManager.instance().getSortOrderList(DataManager.instance().mDealInfoList));
        notifyDataChanged();
    }

    public int getItemCount() {
        if(null != mList){
            return mList.getDataCount();
        }
        return 0;
    }

    public DataItemDetail getItem(int position) {
        if (position < 0 || position >= mList.getDataCount()) {
            return null;
        }

        if(null !=  mList && null != mList.getItem(position)){
            return mList.getItem(position);
        }

        return null;
    }

    @Override
    public void updateViews(RecyclerView.ViewHolder viewholder, int position) {
        mItem = getItem(position);
        ListItemView mItemView = (ListItemView) viewholder;
        if(null != mItem) {
            updateViews(mItemView);
        }
    }

    @Override
    public void updateViews(RecyclerView.ViewHolder viewholder, List payloads) {

    }

    /**
     * 刷新界面
     * @param mItemView
     */
    private void updateViews(ListItemView mItemView){
        mItemView.prdnameV.setText(DataManager.instance().getPrdName(mItem));
        mItemView.timeV.setText(StringFormatter.instance().secToTime(mItem.getInt(GTSConst.JSON_KEY_TIME)));
        mItemView.orderIdV.setText("#" + String.valueOf(mItem.getString(GTSConst.JSON_KEY_ID)));

        m_spannableTitle.reset();
        if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_MARKET_TYPE
                || mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_APPLY_TYPE
                || mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_APPLY_STOP_TYPE) {
            m_spannableTitle.append(AppMain.getAppString(R.string.order_type_create));
            if (mItem.getInt(GTSConst.JSON_KEY_DIRECTION) == AppContances.TRADE_TYPE_BUY) {
                m_spannableTitle.append(AppMain.getAppString(R.string.order_trade_type_buy));
            } else {
                m_spannableTitle.append(AppMain.getAppString(R.string.order_trade_type_sale));
            }
        } else {
            m_spannableTitle.append(AppMain.getAppString(R.string.order_type_close_market));
            if (mItem.getInt(GTSConst.JSON_KEY_DIRECTION) == AppContances.TRADE_TYPE_BUY) {
                m_spannableTitle.append(AppMain.getAppString(R.string.order_trade_type_buy));
            } else {
                m_spannableTitle.append(AppMain.getAppString(R.string.order_trade_type_sale));
            }
        }
        mItemView.dirV.setText(m_spannableTitle.getBuilder());

        if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_MARKET_TYPE
                || mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_CLOSE_TYPE
                || mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_PART_CLOSE_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason1));
        } else if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_APPLY_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason2));
        } else if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_LIMIT_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason6));
        } else if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_APPLY_STOP_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason3));
        } else if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_STOP_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason5));
        } else if (mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.ORDER_TYPE_FORCE_CLOSE_TYPE|| mItem.getInt(GTSConst.JSON_KEY_DEALTYPE) == AppContances.SYS_ORDER_TYPE_FORCE_CLOSE_TYPE) {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason4));
        } else {
            mItemView.typeV.setText(AppMain.getAppString(R.string.order_trade_reason1));
        }

        m_spannableTitle.reset();
        m_spannableTitle.append(AppMain.getAppString(R.string.order_trade_lot, mItem.getString(GTSConst.JSON_KEY_VOLUME)));
        m_spannableTitle.append("@" + mItem.getString(GTSConst.JSON_KEY_DEALPRICE));
        mItemView.lotV.setText(m_spannableTitle.getBuilder());
    }

    @Override
    public int getLayoutID() {
        return R.layout.list_item_deal;
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View v, int type) {
        return new ListItemView(v);
    }

    public class ListItemView extends RecyclerView.ViewHolder{
        /** 产品名称 */
        @BindView(R.id.trade_prd_name)
        public TextView prdnameV;
        /** 买卖方向 */
        @BindView(R.id.trade_dir)
        public TextView dirV;
        /** 订单号 */
        @BindView(R.id.trade_order_id)
        public TextView orderIdV;
        /** 类型 */
        @BindView(R.id.trade_type)
        public TextView typeV;
        /** 手数 */
        @BindView(R.id.trade_lot)
        public TextView lotV;
        /** 时间 */
        @BindView(R.id.trade_time)
        public TextView timeV;

        public ListItemView(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
